//
//  IGActivityCell.m
//  jingdongfang
//
//  Created by BOE on 2018/5/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGActivityCell.h"

@interface IGActivityCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *typeLabel;

@property (nonatomic, strong) YYLabel *titleLabel;
@property (nonatomic, strong) YYLabel *detailLabel;
@property (nonatomic, strong) UIButton *attentionBtn;

@end

@implementation IGActivityCell

+ (IGActivityCell *)cellWithTableView:(UITableView *)tableView
{
    IGActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGActivityCell];
    if (!cell) {
        cell = [[IGActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGActivityCell];
        cell.contentView.backgroundColor = [UIColor mainBackgroundColor];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self createUI];
    }
    return self;
}

- (void)createUI {
    UIView *bgView = [UIView new];
    _bgView = bgView;
    bgView.backgroundColor = [UIColor c3Color];
    [self.contentView addSubview:bgView];
    
    _imgView = [UIImageView new];
    [self.contentView addSubview:_imgView];
    _imgView.backgroundColor = [UIColor mainRandomColor];
    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(203);
    }];

    _typeLabel = [UILabel new];
    _typeLabel.font = [UIFont font13];
    _typeLabel.textColor = [UIColor c3Color];
    _typeLabel.backgroundColor = [UIColor colorWithRed:218.0/255.0 green:197.0/255.0 blue:142.0/255.0 alpha:1.0];
    _typeLabel.textAlignment = NSTextAlignmentCenter;
    [_imgView addSubview:_typeLabel];
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-8);
    }];
    
    _attentionBtn = [UIButton new];
    [self.contentView addSubview:_attentionBtn];
    _attentionBtn.clipsToBounds=YES;
    _attentionBtn.layer.borderWidth=1.0f;
    _attentionBtn.layer.cornerRadius=15.f;
    _attentionBtn.layer.borderColor=[UIColor c1Color].CGColor;
    _attentionBtn.contentEdgeInsets=UIEdgeInsetsMake(5, 10, 5, 10);
    _attentionBtn.titleLabel.font=[UIFont font14];
    [_attentionBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
    [_attentionBtn setTitleColor:[UIColor c1Color] forState:UIControlStateNormal];
    [_attentionBtn setTitle:@"已关注" forState:UIControlStateSelected];
    [_attentionBtn setTitle:@"+ 关注" forState:UIControlStateNormal];
    [_attentionBtn addTarget:self action:@selector(attentionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionBtn setSelected:NO];
    [_attentionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-13);
        make.top.mas_equalTo(_imgView.mas_bottom).mas_offset(16);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
    
    _titleLabel = [YYLabel new];
    _titleLabel.font = [UIFont systemFontOfSize:20];
    _titleLabel.textColor = [UIColor c4Color];
    _titleLabel.numberOfLines = 2;
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(_attentionBtn.mas_left).mas_offset(-20);
        make.top.mas_equalTo(_imgView.mas_bottom).mas_offset(13.5);
        make.height.mas_equalTo(10);
    }];

    _detailLabel = [YYLabel new];
    _detailLabel.font = [UIFont font13];
    _detailLabel.textColor = [UIColor c5Color];
    _detailLabel.numberOfLines = 0;
    [self.contentView addSubview:_detailLabel];
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(_titleLabel.mas_bottom).mas_offset(15.5);
        make.height.mas_equalTo(10);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imgView.mas_top);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(_detailLabel.mas_bottom).mas_offset(15);
    }];
}

- (void)attentionBtnClick:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    if ([sender isSelected]) {
        sender.layer.borderColor=[UIColor c5Color].CGColor;
    }else{
        sender.layer.borderColor=[UIColor c1Color].CGColor;
    }
    if (self.attentionBlock) {
        self.attentionBlock();
    }
}

- (void)setActModel:(IGActivityModel *)actModel {
    [_imgView sd_setImageWithURL:[NSURL URLWithString:actModel.image]];
    _typeLabel.text = actModel.type;
    _attentionBtn.selected = [actModel.attentionState boolValue];
    if (_attentionBtn.selected) {
        _attentionBtn.layer.borderColor=[UIColor c5Color].CGColor;
    } else {
        _attentionBtn.layer.borderColor=[UIColor c1Color].CGColor;
    }
    NSDictionary *attributesT = @{NSFontAttributeName:[UIFont font13]};
    CGSize sizeType = [actModel.type boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesT context:nil].size;
    [_typeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(sizeType.width+5);
    }];
    
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc]init];
    muStyle.lineSpacing = 3;    //设置行间距离
    muStyle.alignment = NSTextAlignmentLeft;//对齐方式
    
    NSString *title = actModel.title;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title];
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, attrString.length)];
    [attrString addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, attrString.length)];
    self.titleLabel.attributedText = attrString;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20], NSParagraphStyleAttributeName:muStyle};
    CGSize sizeTitle = [title boundingRectWithSize:CGSizeMake(self.width - 15 - 53, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(sizeTitle.height+0.5);
    }];
    
    NSString *detail = actModel.adescription;
    NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:detail];
    [attrString1 addAttribute:NSFontAttributeName value:[UIFont font13] range:NSMakeRange(0, attrString1.length)];
    [attrString1 addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, attrString1.length)];
    self.detailLabel.attributedText = attrString1;
    NSDictionary *attributes1 = @{NSFontAttributeName:[UIFont font13], NSParagraphStyleAttributeName:muStyle};
    CGSize sizeDetail = [detail boundingRectWithSize:CGSizeMake(self.width - 15 - 15, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes1 context:nil].size;
    [_detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(sizeDetail.height+0.5);
    }];
}

+ (CGFloat)cellHeight:(IGActivityModel *)actModel
{
    CGFloat h = 20 + 203 + 13.5 + 14.5 + 14.5;
    
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc]init];
    muStyle.lineSpacing = 3;    //设置行间距离
    muStyle.alignment = NSTextAlignmentLeft;//对齐方式

    NSString *title = actModel.title;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title];
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, attrString.length)];
    [attrString addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, attrString.length)];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20], NSParagraphStyleAttributeName:muStyle};
    CGSize sizeTitle = [title boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 35 - 15 - 53, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    h = h + sizeTitle.height+0.5;
    
    NSString *detail = actModel.adescription;
    NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:detail];
    [attrString1 addAttribute:NSFontAttributeName value:[UIFont font13] range:NSMakeRange(0, attrString1.length)];
    [attrString1 addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, attrString1.length)];
    NSDictionary *attributes1 = @{NSFontAttributeName:[UIFont font13], NSParagraphStyleAttributeName:muStyle};
    CGSize sizeDetail = [detail boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 35 - 15 - 15, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes1 context:nil].size;
    h = h + sizeDetail.height+0.5;
    
    return h;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
